//
//  Profile.m
//  Opper
//
//  Created by Le Dung on 4/12/13.
//  Copyright (c) 2013 LeDung. All rights reserved.
//

#import "Profile.h"
#define xLabel 26
#define yLabel 150
@interface Profile ()

@end
@implementation Profile
@synthesize containView;
@synthesize isMove;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)viewDidAppear:(BOOL)animated
{

    [StyleClass barButtonItemEdit:self left:YES right:NO];
    
}
-(void)rightPress
{

}

-(void)leftPress
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)acceptPress
{
    Member *member =[[Member alloc ] init ];
    member.firstName=self.firstName.text;
    member.midleName=self.midName.text;
    member.lastname=self.lastName.text;
    member.gender= [NSString stringWithFormat:@"%d",self.isGender ];
    member.address=self.address.text;
    member.houseNumber=self.houseNumber.text;
    member.postCode=self.postCode.text;
    member.place=self.place.text;
    member.phoneNumber=self.phoneNumber.text;
    member.birthDay=self.dateButton.titleLabel.text;
    
    NSString *functionName =@"addMember";
    NSMutableDictionary *dictionary =[[[NSMutableDictionary alloc]init] autorelease];
    [dictionary setValue:[GetData App].userAccount.userName forKey:@"username"];
    [dictionary  setValue:[GetData App].userAccount.password forKey:@"passwd"];
    
    [dictionary setValue:member.firstName forKey:@"first_name"];
    [dictionary  setValue:member.lastname forKey:@"last_name"];
    [dictionary setValue:member.midleName forKey:@"insertion"];
    [dictionary  setValue:member.gender forKey:@"gender"];
    [dictionary setValue:member.address forKey:@"address"];
    [dictionary  setValue:member.houseNumber forKey:@"house_number"];
    [dictionary setValue:member.postCode forKey:@"postcode"];
    [dictionary setValue:member.place forKey:@"place"];
    [dictionary  setValue:@"mail" forKey:@"email"];
    [dictionary setValue:member.phoneNumber forKey:@"phone"];
    [dictionary  setValue:member.birthDay forKey:@"birthday"];

    
    NSString *jsonRespones =[GetData sendRequest:functionName andDictionary:dictionary];
    if(jsonRespones)
    {
        if(![GetData checkError:jsonRespones])
        {
            [GetData getListProfile];
            [self.navigationController popViewControllerAnimated:YES];
        }
        else
        {
            NSString *massage =[GetData checkMassage:jsonRespones];
            UIAlertView *alerView =[[UIAlertView alloc]
                                    initWithTitle:@"Error" message:massage delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil ];
            [alerView show];
            [alerView release];
        }
    }
}

- (IBAction)dateButton:(id)sender
{
    [self.dateView setHidden:NO];
}

-(void)cancelPress
{
    [self.navigationController  popViewControllerAnimated:YES];
}


-(void)textFieldDidEndEditing:(UITextField *)textField
{
    
}
-(void)buttonTarget
{
    [self.acceptButton addTarget:self action:@selector(acceptPress) forControlEvents:UIControlEventTouchUpInside];
    [self.acceptButton setBackgroundImage:[StyleClass nGreen] forState:UIControlStateNormal];
    [self.cancelButton addTarget:self action:@selector(cancelPress) forControlEvents:UIControlEventTouchUpInside];
    [self.cancelButton setBackgroundImage:[StyleClass nRed] forState:UIControlStateNormal];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self buttonTarget];
    [StyleClass background:self.view];
    [StyleClass createNavi:self andTitle:@"Profile"];
    [self drawLine];
    [self createDatePicker];

    
    
        
    [self style];
}


-(void)createDatePicker
{
    self.datePiker =[[UIDatePicker alloc]init];
    self.datePiker.datePickerMode= UIDatePickerModeDate;
    [self.datePiker addTarget:self action:@selector(pickerPress) forControlEvents:UIControlEventValueChanged];
    
    
    CGRect frame ;
    frame.size.width=300;
    frame.size.height =150;
    frame.origin.x=0;
    frame.origin.y=0;
    self.datePiker.frame =frame;
    
    self.dateView =[[UIView alloc]init];
    frame.size.height=200;
    frame.origin.x =10;
    self.dateView.frame =frame;
    [self.view addSubview:self.dateView];
    [self.dateView addSubview:self.datePiker];
    [self.datePiker release];
    [self.dateView setBackgroundColor:[UIColor whiteColor]];
    self.dateView.layer.borderColor=[UIColor clearColor].CGColor;
    self.dateView.layer.borderWidth=1;
    
    
    UIButton *buton =[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [buton setFrame:CGRectMake(0,165, 300, 30)];
    [buton setTitle:@"Accept" forState:UIControlStateNormal];
    [buton addTarget:self  action:@selector(datePikerHide) forControlEvents:UIControlEventTouchUpInside];
    [buton setBackgroundImage:[StyleClass nGreen] forState:UIControlStateNormal];
    [self.dateView addSubview:buton];
    [self.dateView setHidden:YES];
    [self.dateView release];
}

-(void)pickerPress
{
    NSDate *currDate = [self.datePiker date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"dd-MM-YYYY"];
    NSString *dateString = [dateFormatter stringFromDate:currDate];
    [self.dateButton setTitle:dateString forState:UIControlStateNormal];

    
}


-(void)datePikerHide
{
    if(self.dateView.hidden)
    {
        [self.dateView setHidden:NO];
    }
    else
    {
        [self.dateView setHidden:YES];
    }
}

-(void)style
{
    self.containView.userInteractionEnabled =YES;
    [self.containView sizeToFit];
    self.containView.layer.shadowColor = [[UIColor blackColor] CGColor];
    self.containView.layer.shadowOffset = CGSizeMake(0.0, 3.0);
    self.containView.layer.shadowOpacity = 0;
}


///100 < button tag < 150;
-(void)drawLine
{
    for(int i = 0 ; i<10; i++)
    {
        UIImageView *line=[[UIImageView alloc]initWithFrame:CGRectMake(0, 40+i*30, 150, 1)];
        line.backgroundColor =[UIColor clearColor];
        [self.containView addSubview:line];
        [line release];
        UIImageView *line2=[[UIImageView alloc]initWithFrame:CGRectMake(160, 40+i*30, 140, 1)];
        line2.backgroundColor =[UIColor clearColor];
        [self.containView addSubview:line2];
        [line2 release];
    }
}

-(IBAction)genderSelect:(id)sender
{
    UIButton *button =(UIButton *)sender;
    if(button.tag==101)
    {
        [button setAlpha:1];
        self.isGender =1;
        UIButton *buttonAl =(UIButton *)[self.view viewWithTag:102];
        [buttonAl setAlpha:0.5];
    }
    if(button.tag==102)
    {
        self.isGender =0;
        NSLog(@"sdf");
        [button setAlpha:1];
        UIButton *buttonAl =(UIButton *)[self.view viewWithTag:101];
        [buttonAl setAlpha:0.5];
    }
    
}

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    if(!isMove &&textField.frame.origin.y>140)
    {
        [StyleClass moveUp:self.containView andYLocation:160];
        isMove=1;
    }
    
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if(isMove)
    {
        [StyleClass moveDown:self.containView andYLocation:160];
        isMove=0;
    }
    
    [textField resignFirstResponder];
    return YES;
}

-(void)labelFormat:(UILabel*)label
{
    label.font = [UIFont systemFontOfSize:15];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [containView release];
    [_acceptButton release];
    [_cancelButton release];
    [_dateButton release];
    [super dealloc];

}
@end
